package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.LeaseOrderAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.LeaseMainList;
import com.isunland.managesystem.entity.LeaseMainListOriginal;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能: 预约下单
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/1/3 14:20
 */

public class LeaseOrderListFragment extends BaseListFragment {
  private static final int REQUEST_ADD = 3;
  private static final int REQUEST_DETAIL = 4;
  private ArrayList<LeaseMainList> mList;
  private LeaseOrderAdapter<LeaseMainList> mAdapter;

  /**
   * Mandatory empty constructor for the fragment manager to instantiate the
   * fragment (e.g. upon screen orientation changes).
   */
  public LeaseOrderListFragment() {
  }

  //public static AppointementOrderListFragment newInstance(int columnCount) {
  //    AppointementOrderListFragment fragment = new AppointementOrderListFragment();
  //    Bundle args = new Bundle();
  //    args.putInt(ARG_COLUMN_COUNT, columnCount);
  //    fragment.setArguments(args);
  //    return fragment;
  //}

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    BaseVolleyActivity mActivity = (BaseVolleyActivity) getActivity();
    ActionBar actionBar = mActivity.getSupportActionBar();

    if (actionBar != null) {
      actionBar.setTitle(R.string.leaseManagement);
      actionBar.setDisplayHomeAsUpEnabled(true);
    }
    mList = new ArrayList<>();

    mAdapter = new LeaseOrderAdapter<>(mActivity, mList);
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    int padding = MyUtils.dip2px(getActivity().getApplicationContext(), 16);
    //noinspection deprecation
    mListview.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
    mListview.setDividerHeight(padding);
    mListview.setPadding(0, 0, 0, padding);

    setListAdapter(mAdapter);
  }

  @Override public String getUrl() {
    return ApiConst.URL_LEASE_LIST;
    //return ApiConst.URL_LATEST_VERSION;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("jobNo", CurrentUser.newInstance(mActivity).getJobNumber());//jobNo 1000622693
    return params;
  }

  @Override public void resolveResponse(String json) {
    //json = MyUtils.readAssetsFile(mActivity, "testAppointmentOrder.json");
    LeaseMainListOriginal result = new Gson().fromJson(json, LeaseMainListOriginal.class);

    if (result.getResult() == Base.SUCCESS && result.getRows() != null) {
      //if (result.getRows() != null) {
      //获取列表成功
      //LogUtil.i("date="+result.getRows().get(0).getRegDate());
      mList.clear();
      mList.addAll(result.getRows());
      mAdapter.notifyDataSetChanged();
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;

    if (requestCode == REQUEST_ADD) {

      volleyPost();
      return;
    }
    if (requestCode == REQUEST_DETAIL) {

      volleyPost();
      return;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    LeaseMainList item = mAdapter.getItem(position - mListview.getHeaderViewsCount());

    LeaseOrderPagerActivity.newInstance(this, mActivity, item, REQUEST_DETAIL);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        break;
      case R.id.menu_item_add:
        LeaseOrderUpdateActivity.newInstance(this, mActivity, null, REQUEST_ADD);

        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  //@Override
  //public void click(View v) {
  //    if (v == null) return;
  //    switch (v.getId()) {
  //        case R.id.tv_cancel://取消
  //            if (v.getTag() instanceof Integer) {
  //                int position = (Integer) v.getTag();
  //                LeaseMainList item = mAdapter.getItem(position);
  //                ToastUtil.show("取消" + item.getId());
  //            }
  //            return;
  //        case R.id.tv_reminder://催单
  //            if (v.getTag() instanceof Integer) {
  //                int position = (Integer) v.getTag();
  //                LeaseMainList item = mAdapter.getItem(position);
  //                ToastUtil.show("催单" + item.getId());
  //            }
  //            return;
  //        default:
  //            break;
  //    }
  //}
}
